package com.cy.cy2025.activity

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.asLiveData
import androidx.lifecycle.lifecycleScope
import com.cy.cy2025.R
import com.cy.cy2025.activity.LiveFlowActivity.Companion.testLivedata
import com.cy.cy2025.activity.LiveFlowActivity.Companion.testSharedFlow
import com.cy.cy2025.activity.LiveFlowActivity.Companion.testStateFlow
import com.cy.cy2025.activity.LiveFlowActivity.Companion.testUpdate
import com.cy.cy2025.box.Box.once
import com.cy.cy2025.databinding.ActivityLiveFlow2Binding
import com.cy.cy2025.databinding.ActivityLiveFlow3Binding
import com.cy.cy2025.databinding.ActivityLiveFlowBinding
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch

class LiveFlowActivity : AppCompatActivity() {


    companion object {

        var x = 0

        val testLivedata = MutableLiveData<String>()

        val testStateFlow = MutableStateFlow<String>("testStateFlow")

        val testSharedFlow = MutableSharedFlow<String>()


        suspend fun testUpdate() {
            if (x < 10) {
                x++
            }
            testLivedata.once("$x")
            testStateFlow.value = "$x"
            testSharedFlow.emit("$x")
        }

    }

    val binding by lazy {
        ActivityLiveFlowBinding.inflate(layoutInflater)
    }


    val str = """
        LiveData: 可以不设置初始值，同对象可以触发更新
        StateFlow: 必须设置初始值，同对象不会触发更新
        SharedFlow: 
        无初始值，默认重复数和缓冲数均为0，所以更新事件只会响应一次，相当于单播
        可设置重放数和缓冲数，
        同对象可以触发更新
        
    """.trimIndent()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(binding.root)

        initObserver()

        binding.button.setOnClickListener {
            lifecycleScope.launch {
                testUpdate()
            }
        }
        binding.btnStart.setOnClickListener {
            startActivity(Intent(this, LiveFlowActivity2::class.java))
        }

        binding.tv.text = str
    }

    private fun initObserver() {
        testLivedata.observe(this) {
            it?.let {
                Log.i("LiveFlowActivity", "testLivedata收到更新: $it")
            }

        }

        testStateFlow.asLiveData().observe(this) {
            Log.i("LiveFlowActivity", "testStateFlow收到更新: $it")
        }

        testSharedFlow.asLiveData().observe(this) {
            Log.i("LiveFlowActivity", "testSharedFlow收到更新: $it")
        }
    }
}


class LiveFlowActivity2 : AppCompatActivity() {

    val binding by lazy {
        ActivityLiveFlow2Binding.inflate(layoutInflater)
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(binding.root)

        initObserver()

        binding.button.setOnClickListener {
            lifecycleScope.launch {
                testUpdate()
            }
        }
        binding.btnStart.setOnClickListener {
            startActivity(Intent(this, LiveFlowActivity3::class.java))
        }
    }

    private fun initObserver() {
        testLivedata.observe(this) {
            Log.i("LiveFlowActivity2", "testLivedata收到更新: $it")
        }

        testStateFlow.asLiveData().observe(this) {
            Log.i("LiveFlowActivity2", "testStateFlow收到更新: $it")
        }

        testSharedFlow.asLiveData().observe(this) {
            Log.i("LiveFlowActivity2", "testSharedFlow收到更新: $it")
        }
    }
}


class LiveFlowActivity3 : AppCompatActivity() {

    val binding by lazy {
        ActivityLiveFlow3Binding.inflate(layoutInflater)
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContentView(binding.root)

        initObserver()

        binding.button.setOnClickListener {
            lifecycleScope.launch {
                testUpdate()
            }
        }
    }

    private fun initObserver() {
        testLivedata.observe(this) {
            Log.i("LiveFlowActivity3", "testLivedata收到更新: $it")
        }

        testStateFlow.asLiveData().observe(this) {
            Log.i("LiveFlowActivity3", "testStateFlow收到更新: $it")
        }

        testSharedFlow.asLiveData().observe(this) {
            Log.i("LiveFlowActivity3", "testSharedFlow收到更新: $it")
        }
    }
}